package net.office.service.impl;

import net.office.bean.Expense;
import net.office.bean.ExpenseExample;
import net.office.mapper.ExpenseMapper;
import net.office.service.ExpenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExpenseServiceImpl implements ExpenseService {

    @Autowired
    private ExpenseMapper expenseMapper;

    @Override
    public List<Expense> findAll() {
        ExpenseExample example = new ExpenseExample();
        return expenseMapper.selectByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer expenseId) {
        return expenseMapper.deleteByPrimaryKey(expenseId);
    }

    @Override
    public List<Expense> findByExpense(Expense expense) {
        ExpenseExample example = new ExpenseExample();
        ExpenseExample.Criteria criteria = example.createCriteria();

        if(expense.getExpenseId() != null){
            criteria.andExpenseIdEqualTo(expense.getExpenseId());
        }
        if(expense.getExpenseDepartment() != null
                && expense.getExpenseDepartment().trim().length() != 0){
            criteria.andExpenseDepartmentLike("%" + expense.getExpenseDepartment() + "%");
        }
        if(expense.getExpenseDealName() != null
                && expense.getExpenseDealName().trim().length() != 0){
            criteria.andExpenseDealNameLike("%" + expense.getExpenseDealName() + "%");
        }
        if(expense.getExpenseTotal() != null){//~
            criteria.andExpenseTotalEqualTo(expense.getExpenseTotal());
        }
        if(expense.getExpenseWay() != null){
            criteria.andExpenseWayEqualTo(expense.getExpenseWay());
        }
        if(expense.getExpenseReceiveDepartment() != null
                && expense.getExpenseReceiveDepartment().trim().length() != 0){
            criteria.andExpenseReceiveDepartmentLike("%" + expense.getExpenseReceiveDepartment() + "%");
        }
        if(expense.getExpenseReceiveAccount() != null
                && expense.getExpenseReceiveAccount().trim().length() != 0){
            criteria.andExpenseReceiveAccountLike("%" + expense.getExpenseReceiveAccount() + "%");
        }
        if(expense.getExpenseReceiveBank() != null
                && expense.getExpenseReceiveBank().trim().length() != 0){
            criteria.andExpenseReceiveBankLike("%" + expense.getExpenseReceiveBank() + "%");
        }
        if(expense.getGeneralManagerStaffId() != null){
            criteria.andGeneralManagerStaffIdEqualTo(expense.getGeneralManagerStaffId());
        }
        if(expense.getGeneralManagerStaffName() != null
                && expense.getGeneralManagerStaffName().trim().length() != 0){
            criteria.andGeneralManagerStaffNameLike("%" + expense.getGeneralManagerStaffName() + "%");
        }
        if(expense.getDepartManagerStaffId() != null){
            criteria.andDepartManagerStaffIdEqualTo(expense.getDepartManagerStaffId());
        }
        if(expense.getDepartManagerStaffName() != null
                && expense.getDepartManagerStaffName().trim().length() != 0){
            criteria.andDepartManagerStaffNameLike("%" + expense.getDepartManagerStaffName() + "%");
        }
        if(expense.getAuditorStaffId() != null){
            criteria.andAuditorStaffIdEqualTo(expense.getAuditorStaffId());
        }
        if(expense.getAuditorStaffName() != null
                && expense.getAuditorStaffName().trim().length() != 0){
            criteria.andAuditorStaffNameLike("%" + expense.getAuditorStaffName() + "%");
        }
        if(expense.getExpenseStaffId() != null){
            criteria.andExpenseStaffIdEqualTo(expense.getExpenseStaffId());
        }
        if(expense.getExpenseStaffNo() != null
                && expense.getExpenseStaffNo().trim().length() != 0){
            criteria.andExpenseStaffNoLike("%" + expense.getExpenseStaffNo() + "%");
        }
        if(expense.getExpenseStaffName() != null
                && expense.getExpenseStaffName().trim().length() != 0){
            criteria.andExpenseStaffNameLike("%" + expense.getExpenseStaffName() + "%");
        }
        if(expense.getExpenseResult() != null){
            criteria.andExpenseResultEqualTo(expense.getExpenseResult());
        }

        example.or(criteria);

        return expenseMapper.selectByExample(example);
    }

    @Override
    public int insert(Expense expense) {
        return expenseMapper.insertSelective(expense);
    }

    @Override
    public Expense findOneByExpenseId(Integer expenseId) {
        return expenseMapper.selectByPrimaryKey(expenseId);
    }

    @Override
    public int updateByPrimaryKey(Expense expense) {
        return expenseMapper.updateByPrimaryKey(expense);
    }

    @Override
    public List<Expense> findAllByStaffId(int staffId) {
        ExpenseExample example = new ExpenseExample();
        example.createCriteria().andExpenseStaffIdEqualTo(staffId);

        return expenseMapper.selectByExample(example);
    }
}
